home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / string / strtod.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  2KB  |  109 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *  $Id: strtod.c,v 1.2 1994/07/08 16:26:11 rluebbert Exp $
  21.  *
  22.  *  $Log: strtod.c,v $
  23.  * Revision 1.2  1994/07/08  16:26:11  rluebbert
  24.  * Added NULL definition
  25.  *
  26.  * Revision 1.1  1994/07/08  16:23:54  rluebbert
  27.  * Initial revision
  28.  *
  29.  */
  30.  
  31. #include <stdlib.h>
  32. #include <ctype.h>
  33.  
  34. double
  35. strtod (const char *s, char **ptr)
  36. {
  37.     double ret = 0.0;
  38.     const char *start = s;
  39.     const char *begin = NULL;
  40.     int success = 0;
  41.  
  42.     /* optional white space */
  43.     while (isspace(*s))
  44.         s++;
  45.  
  46.     /* optional sign */
  47.     if (*s == '+' || *s == '-') {
  48.         s++;
  49.         if (*(s-1) == '-')
  50.             begin = s - 1;
  51.         else
  52.             begin = s;
  53.     }
  54.  
  55.     /* string of digits with optional decimal point */
  56.     if (isdigit(*s) && ! begin)
  57.         begin = s;
  58.  
  59.     while (isdigit(*s)) {
  60.         s++;
  61.         success++;
  62.     }
  63.  
  64.     if (*s == '.') {
  65.         if (! begin)
  66.             begin = s;
  67.         s++;
  68.         while (isdigit(*s))
  69.             s++;
  70.         success++;
  71.     }
  72.  
  73.     if (s == start || success == 0)        /* nothing there */
  74.         goto out;
  75.  
  76.     /*
  77.       *    optional 'e' or 'E'
  78.      *        followed by optional sign or space
  79.      *        followed by an integer
  80.      */
  81.  
  82.     if (*s == 'e' || *s == 'E') {
  83.         s++;
  84.  
  85.         /* XXX - atof probably doesn't allow spaces here */
  86.         while (isspace(*s))
  87.             s++;
  88.  
  89.         if (*s == '+' || *s == '-')
  90.             s++;
  91.  
  92.         while (isdigit(*s))
  93.             s++;
  94.     }
  95.  
  96.     /* go for it */
  97.     ret = atof(begin);
  98.  
  99. out:
  100.     if (! success)
  101.         s = start;    /* in case all we did was skip whitespace */
  102.  
  103.     if (ptr)
  104.         *ptr = (char *)s;
  105.  
  106.     return ret;
  107. }
  108.  
  109.